#!/usr/bin/python
# -*- coding:utf-8 -*-
import urllib

from bs4 import BeautifulSoup


class Parser(object):
    def parser(self, html, url):
        """
        网页文档解析器
        :param html: 网页文档
        :param url: 网页的url
        :return: (文档内的链接, 问题字典/None)
        """
        soup = BeautifulSoup(html, 'html.parser', from_encoding='utf-8')

        # 获取链接
        page_elements = soup.find(attrs={'class': 'page'}).contents
        links = list()
        for a in page_elements:
            if hasattr(a, 'next') and '下一页' in a.next:
                links.append('http://' + str(a.get('href').replace('http://', '')[:-3]) + '%E5%9B%9B%E5%B7%9D%E7%9C%81')

            # 获取数据
        talent_list = soup.find_all(attrs={'class': 'talent_list_box borbox'})
        datas = list()
        for talent in talent_list:
            com_txt = talent.find(attrs={'class': 'talent_list_box_com_txt'})

            # print '名称：' + str(talent.find('h3').text)
            # print '类型：' + str(talent_list_box_com_txt.select('span u')[0].next).strip()
            # print '省：' + str(talent_list_box_com_txt.select('span u')[0].find_all('a')[0].text)
            # print '市：' + str(talent_list_box_com_txt.select('span u')[0].find_all('a')[1].text)
            # print '好评：' + str(talent_list_box_com_txt.select('span u')[0].find(attrs={'class': 'c065'}).text)
            # print '服务：' + str(talent_list_box_com_txt.select('span u')[1].find(attrs={'class': 'c065'}).text)
            # print '等级：' + str(talent_list_box_com_txt.find('samp').find('i', attrs={'class': 'iico'}).text)
            # print '信用：' + str(talent_list_box_com_txt.find('b', attrs={'class': 'c36b'}).text)
            # print '能力标签：' + str(talent.find('div', attrs={'class': 'baoqian_li'}).text)

            try:
                data = {
                    'name': str(talent.find('h3').text),  # 名称
                    'url': str(talent.find('h3').find('a').attrs['href']),  # 地址
                    'type': str(com_txt.select('span u')[0].next).strip(),  # 类型
                    'province': str(com_txt.select('span u')[0].find_all('a')[0].text),  # 省
                    'city': str(com_txt.select('span u')[0].find_all('a')[1].text),  # 市
                    'feedback_rate': str(com_txt.select('span u')[0].find('b').text), # 好评率
                    'service': str(com_txt.select('span u')[1].find(attrs={'class': 'c065'}).text),  # 服务
                    'level': str(com_txt.find('samp').find('i', attrs={'class': 'iico'}).text),  # 等级
                    'credit': str(com_txt.find('b', attrs={'class': 'c36b'}).text),  # 信用
                    'label': str(talent.find('div', attrs={'class': 'baoqian_li'}).text)  # 能力标签
                }
                datas.append(data)
            except Exception, e:
                print e
                return links, None

        return links, datas
